﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using KegCommunication;
using KegData;

namespace KegPlugin
{
    public class Plugin : IListenerPlugin
    {
        
        public void Pour(Pour p)
        {
            if (TwitterPlugin.Properties.Settings.Default.EnablePour == true)
            {
                string TwitterUser = TwitterPlugin.Properties.Settings.Default.username;
                string TwitterPassword = TwitterPlugin.Properties.Settings.Default.password;

                string Message = TwitterPlugin.Properties.Settings.Default.PourString;
                Message = Message.Replace("<volume>", p.Volume.ToString("#0.00"));
                Message = Message.Replace("<user>", p.User.Name);
                Message = Message.Replace("<beer>", p.Keg.Beer.Name);

                //PostTwitterUpdate(TwitterUser, TwitterPassword, Message);
            }
        }

        public void Temperature(Temperature t)
        {   
        }

        public void Settings()
        {
            TwitterPlugin.TwitterSettings ts = new TwitterPlugin.TwitterSettings();
            ts.ShowDialog();
        }

        public void PostTwitterUpdate(string userName, string password, string updateMessage)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Credentials = new NetworkCredential(userName, password);
                ServicePointManager.Expect100Continue = false;

                byte[] updateMessageBytes = System.Text.Encoding.UTF8.GetBytes("status=" + updateMessage); //Use UTF8 to get it properly encoded if you use characters like ç ã etc...

                try
                {
                    wc.UploadData("http://twitter.com/statuses/update.xml", updateMessageBytes);
                }
                catch (Exception e)
                {

                }
            }
        }

        private static void PostTweet(string username, string password, string tweet)
        {
            try
            {
                // encode the username/password
                string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
                // determine what we want to upload as a status
                byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
                // connect with the update page
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
                // set the method to POST
                request.Method = "POST";
                request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
                // set the authorisation levels
                request.Headers.Add("Authorization", "Basic " + user);
                request.ContentType = "application/x-www-form-urlencoded";
                // set the length of the content
                request.ContentLength = bytes.Length;

                // set up the stream
                Stream reqStream = request.GetRequestStream();
                // write to the stream
                reqStream.Write(bytes, 0, bytes.Length);
                // close the stream
                reqStream.Close();
            }
            catch (Exception ex) {/* DO NOTHING */}
        }
    }
}
